package com.SouthernWall_404.age_tool.common.Network;

import com.SouthernWall_404.age_tool.AgeTool;
import com.SouthernWall_404.age_tool.common.Network.Packets.C2S.HandShake.MimicBlockRequireHandShakePacket;
import com.SouthernWall_404.age_tool.common.Network.Packets.C2S.HandShake.MusicBlockNBTHandShakePacket;
import com.SouthernWall_404.age_tool.common.Network.Packets.C2S.HandShake.MusicBlockStartPlayHandShakePacket;
import com.SouthernWall_404.age_tool.common.Network.Packets.C2S.MusicBlockNBTC2SPacket;
import com.SouthernWall_404.age_tool.common.Network.Packets.S2C.MimicBlockRespondS2CPacket;
import com.SouthernWall_404.age_tool.common.Network.Packets.S2C.MusicBlockNBTS2CPacket;
import com.SouthernWall_404.age_tool.common.Network.Packets.S2C.MusicBlockStartPlayS2CPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.network.simple.SimpleChannel;

public class ModChannels {

    private static final String PROTOCOL_VERSION = "1";
    private static int id=0;


    public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
            new ResourceLocation(AgeTool.MODID, "main"),
            () -> PROTOCOL_VERSION,
            PROTOCOL_VERSION::equals,
            PROTOCOL_VERSION::equals
    );

    public static void register()
    {
        INSTANCE.registerMessage(
                id++,
                MusicBlockNBTC2SPacket.class,      // 客户端发送给服务端的数据包类
                MusicBlockNBTC2SPacket::encode,    // 序列化方法
                MusicBlockNBTC2SPacket::new,       // 反序列化构造函数
                MusicBlockNBTC2SPacket::handle     // 服务端处理逻辑
        );
        INSTANCE.registerMessage(
                id++,
                MusicBlockNBTS2CPacket.class,      // 客户端发送给服务端的数据包类
                MusicBlockNBTS2CPacket::encode,    // 序列化方法
                MusicBlockNBTS2CPacket::new,       // 反序列化构造函数
                MusicBlockNBTS2CPacket::handle     // 服务端处理逻辑
        );
        INSTANCE.registerMessage(
                id++,
                MusicBlockNBTHandShakePacket.class,      // 客户端发送给服务端的数据包类
                MusicBlockNBTHandShakePacket::encode,    // 序列化方法
                MusicBlockNBTHandShakePacket::new,       // 反序列化构造函数
                MusicBlockNBTHandShakePacket::handle     // 服务端处理逻辑
        );

        INSTANCE.registerMessage(
                id++,
                MusicBlockStartPlayHandShakePacket.class,      // 客户端发送给服务端的数据包类
                MusicBlockStartPlayHandShakePacket::encode,    // 序列化方法
                MusicBlockStartPlayHandShakePacket::new,       // 反序列化构造函数
                MusicBlockStartPlayHandShakePacket::handle     // 服务端处理逻辑
        );

        INSTANCE.registerMessage(
                id++,
                MusicBlockStartPlayS2CPacket.class,      // 客户端发送给服务端的数据包类
                MusicBlockStartPlayS2CPacket::encode,    // 序列化方法
                MusicBlockStartPlayS2CPacket::new,       // 反序列化构造函数
                MusicBlockStartPlayS2CPacket::handle     // 服务端处理逻辑
        );

        INSTANCE.registerMessage(
                id++,
                MimicBlockRequireHandShakePacket.class,      // 客户端发送给服务端的数据包类
                MimicBlockRequireHandShakePacket::encode,    // 序列化方法
                MimicBlockRequireHandShakePacket::new,       // 反序列化构造函数
                MimicBlockRequireHandShakePacket::handle     // 服务端处理逻辑
        );

        INSTANCE.registerMessage(
                id++,
                MimicBlockRespondS2CPacket.class,      // 客户端发送给服务端的数据包类
                MimicBlockRespondS2CPacket::encode,    // 序列化方法
                MimicBlockRespondS2CPacket::new,       // 反序列化构造函数
                MimicBlockRespondS2CPacket::handle     // 服务端处理逻辑
        );



    }


    public static <MSG> void sendToServer(MSG message)
    {
        INSTANCE.sendToServer(message);
    }
    public static <MSG> void sendToClient(MSG message, Player player){INSTANCE.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), message);}

}
